package com.party.core.service.crowdfund.impl;

import com.alibaba.fastjson.JSONObject;
import com.party.common.constant.Constant;
import com.party.common.utils.BigDecimalUtils;
import com.party.core.model.activity.Activity;
import com.party.core.model.crowdfund.Project;
import com.party.core.model.crowdfund.Support;
import com.party.core.model.crowdfund.TargetProject;
import com.party.core.model.distributor.DistributorCount;
import com.party.core.model.distributor.DistributorDetail;
import com.party.core.model.order.*;
import com.party.core.service.activity.IActivityService;
import com.party.core.service.crowdfund.IProjectService;
import com.party.core.service.crowdfund.ISupportRefundService;
import com.party.core.service.crowdfund.ISupportService;
import com.party.core.service.crowdfund.ITargetProjectService;
import com.party.core.service.distributor.IDistributorCountService;
import com.party.core.service.distributor.IDistributorDetailService;
import com.party.core.service.member.biz.MemberPersonalCountBizService;
import com.party.core.service.order.IOrderFormService;
import com.party.core.service.order.IOrderRefundService;
import com.party.core.service.order.IOrderRefundTradeService;
import com.party.pay.model.query.TradeStatus;
import com.party.pay.model.refund.WechatPayRefundResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * 支持退款实现
 * Created by wei.li
 *
 * @date 2017/4/28 0028
 * @time 11:47
 */

@Service
public class SupportRefundService implements ISupportRefundService {

    @Autowired
    private ISupportService supportService;

    @Autowired
    private IProjectService projectService;

    @Autowired
    private ITargetProjectService targetProjectService;

    @Autowired
    private IActivityService activityService;

    @Autowired
    private IOrderRefundService orderRefundService;

    @Autowired
    private IDistributorDetailService distributorDetailService;

    @Autowired
    private IDistributorCountService distributorCountService;

    @Autowired
    private IOrderFormService orderFormService;

    @Autowired
    private IOrderRefundTradeService orderRefundTradeService;

    @Autowired
    private MemberPersonalCountBizService memberPersonalCountBizService;

    protected static Logger logger = LoggerFactory.getLogger(SupportRefundService.class);
    /**
     * 众筹支持退款
     * @param support 支持信息
     * @param refundResponse 退款报文
     */
    @Override
    @Transactional
    public void refund(Support support, WechatPayRefundResponse refundResponse) {
        //订单退款
        orderRefundService.refund(support.getOrderId(), refundResponse);
        //修改业务数据
        support.setPayStatus(Constant.SUPPORT_PAY_STATUS_REFUND);//已经退款
        supportService.update(support);

        Project project = projectService.get(support.getProjectId());
        if (project != null) {
        	project.setFavorerNum(project.getFavorerNum() - 1);//支持人数减

            float num = supportService.sumByProjectId(project.getId());
            project.setRealTimeAmount(num);
            num = num < 0 ? 0 : num;//不能小于0

            //不大于目标金额
            if (num >= project.getTargetAmount()){
                num = project.getTargetAmount();
            }
            else {
                //众筹中
                project.setIsSuccess(Constant.IS_CROWFUND_ING);
            }
        	project.setActualAmount(num);//实际筹款
        	projectService.update(project);

            String orderId = support.getOrderId();
            OrderForm orderForm = orderFormService.get(orderId);

        	TargetProject targetProject = targetProjectService.findByProjectId(project.getId());
        	Activity activity = activityService.get(targetProject.getTargetId());
        	//activity.setFavorerNum(activity.getFavorerNum() - 1);

            activity.setOrderNum(activity.getOrderNum() - 1);
            Double totalMoney = BigDecimalUtils.sub(activity.getTotalMoney(), orderForm.getPayment());
            totalMoney = BigDecimalUtils.round(totalMoney, 2);
            activity.setTotalMoney(totalMoney);

            activityService.update(activity);

            //分销统计更新
            try {
                DistributorDetail distributorDetail = distributorDetailService.findByTargetId(support.getOrderId());
                if (null != distributorDetail){
                    DistributorCount distributorCount = distributorCountService.findByRelationId(distributorDetail.getDistributorRelationId());
                    distributorCount.setFavorerNum(distributorCount.getFavorerNum() - 1);

                    float amount = BigDecimalUtils.sub(distributorCount.getFavorerAmount(), (float)(refundResponse.getRefundFee() * 0.01));
                    amount = BigDecimalUtils.round(amount, 2);
                    distributorCount.setFavorerAmount(amount);
                    distributorCountService.update(distributorCount);
                }
            } catch (Exception e) {
                logger.error("退款分销异常", e);
            }

            try {
                memberPersonalCountBizService.updateCrowdfundMoney(activity.getMember(), project.getAuthorId(), orderForm.getPayment(), false);
            } catch (Exception e) {
                logger.error("实时更新众筹报名金额异常", e);
            }

            try {
                memberPersonalCountBizService.updateSupportMoney(activity.getMember(), support.getFavorerId(), orderForm.getPayment(), false);
                logger.info("执行完毕===========");
            } catch (Exception e) {
                logger.error("实时更新支持金额异常", e);
            }
        }
    }


    /**
     * 众筹支持退款校正
     * @param support
     * @param refundResponse
     */
    @Override
    public void supportRegulateRefund(Support support, WechatPayRefundResponse refundResponse) {
        //修改业务数据
        support.setPayStatus(Constant.SUPPORT_PAY_STATUS_REFUND);//已经退款
        supportService.update(support);

        //订单退款
        OrderForm orderForm = orderFormService.get(support.getOrderId());
        orderForm.setIsPay(PaymentState.NO_PAY.getCode()); // 支付状态更改为未付款
        orderForm.setStatus(OrderStatus.ORDER_STATUS_REFUND.getCode()); // 订单状态更改为已退款
        if (orderForm.getPaymentWay().equals(PaymentWay.WECHAT_PAY.getCode())) {
            orderForm.setTradeState(TradeStatus.WX_REFUND.getCode()); // 交易状态已退款
        } else if (orderForm.getPaymentWay().equals(PaymentWay.ALI_PAY.getCode())) {
            orderForm.setTradeState(TradeStatus.ALI_TRADE_CLOSED.getCode()); // 交易状态已关闭
        }
        orderFormService.update(orderForm);

        //持久化流水信息
        OrderRefundTrade orderTrade = new OrderRefundTrade();
        orderTrade.setOrderFormId(orderForm.getId());
        orderTrade.setOrigin(3);
        orderTrade.setTransactionId(refundResponse.getTransactionId());
        orderTrade.setType(orderForm.getPaymentWay());
        orderTrade.setData(JSONObject.toJSONString(refundResponse));
        orderRefundTradeService.insert(orderTrade);

        Project project = projectService.get(support.getProjectId());
        if (project != null) {
           Integer favorerNum = supportService.countByProject(project.getId());
           project.setFavorerNum(favorerNum);

            float num = supportService.sumByProjectId(project.getId());
            project.setRealTimeAmount(num);
            num = num < 0 ? 0 : num;//不能小于0

            //不大于目标金额
            if (num >= project.getTargetAmount()){
                num = project.getTargetAmount();
            }
            else {
                //众筹中
                project.setIsSuccess(Constant.IS_CROWFUND_ING);
            }
            project.setActualAmount(num);//实际筹款
            projectService.update(project);

            TargetProject targetProject = targetProjectService.findByProjectId(project.getId());
            Activity activity = activityService.get(targetProject.getTargetId());
            //activity.setFavorerNum(activity.getFavorerNum() - 1);

            Map<String, Object> activityData = orderFormService.statisticZcActivityData(activity.getId());
            Long orderNum = activityData.get("orderNum") != null ? (Long) activityData.get("orderNum") : 0L;
            Double totalMoney = activityData.get("totalMoney") != null ? (Double) activityData.get("totalMoney") : 0d;
            activity.setOrderNum(orderNum);
            activity.setTotalMoney(totalMoney);

            activityService.update(activity);

            //分销统计更新
            try {
                DistributorDetail distributorDetail = distributorDetailService.findByTargetId(support.getOrderId());
                if (null != distributorDetail){
                    DistributorCount distributorCount = distributorCountService.findByRelationId(distributorDetail.getDistributorRelationId());
                    distributorCount.setFavorerNum(distributorCount.getFavorerNum() - 1);

                    float amount = BigDecimalUtils.sub(distributorCount.getFavorerAmount(), (float)(refundResponse.getRefundFee() * 0.01));
                    amount = BigDecimalUtils.round(amount, 2);
                    distributorCount.setFavorerAmount(amount);
                    distributorCountService.update(distributorCount);
                }
            } catch (Exception e) {
                logger.error("退款分销异常", e);
            }
        }
    }
}
